package file.config;

import file.Constant;
import file.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 初始化fs.config配置文件
 *
 * @author hh
 * @since 2024/12/8
 */
@Order(2)
@Component
public class FsConfigInitializer implements ApplicationRunner {

    private final Logger log = LoggerFactory.getLogger(FsConfigInitializer.class);

    private final MyConfigProperties myConfigProperties;

    public FsConfigInitializer(MyConfigProperties myConfigProperties) {
        this.myConfigProperties = myConfigProperties;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String realPath = FileUtil.getRealPath(myConfigProperties.getFilePath());
        File directory = new File(realPath);
        if (!directory.exists()) {
            directory.mkdir();
        }
        String path = realPath + FsConfig.CONFIG_FILE_NAME;
        File file = new File(path);
        if (file.exists()) {
            log.info("rs.conf文件已存在，无需初始化！");
            return;
        }
        FileUtil.saveFile(Constant.PWD + "=d98e5c783ba67fb1e3f09d9c7049d61517b7bf69ee2514c3e940020578feb329\n" +
                        Constant.MANAGER_IP + "=" +
                        "\n" + Constant.IP_WHITE_LIST + "=",
                path);
        log.info("rs.conf文件初始化完成！");
        if (myConfigProperties.isEnableLogin()) {
            log.info("初始密码为：666，登录后请及时修改密码！");
        }

    }

}
